//! 中断控制

use bsp_define::{
    irqchip::{IrqType, IrqchipIrqState, Result},
    smp::Cpumask,
};

use crate::bsp::{
    __irqchip_irq_ack, __irqchip_irq_disable, __irqchip_irq_enable, __irqchip_irq_eoi,
    __irqchip_irq_get_irqchip_state, __irqchip_irq_set_affinity, __irqchip_irq_set_irqchip_state,
    __irqchip_irq_set_type, __irqchip_name,
};

/// 中断控制器名字
pub fn irqchip_name() -> &'static str {
    unsafe { __irqchip_name() }
}

/// 应答中断
pub fn irq_ack(hwirq: u32) {
    unsafe {
        __irqchip_irq_ack(hwirq);
    }
}

/// 结束中断
pub fn irq_eoi(hwirq: u32) {
    unsafe {
        __irqchip_irq_eoi(hwirq);
    }
}

/// 禁用中断
pub fn irq_disable(hwirq: u32) {
    unsafe {
        __irqchip_irq_disable(hwirq);
    }
}

/// 激活中断
pub fn irq_enable(hwirq: u32) {
    unsafe {
        __irqchip_irq_enable(hwirq);
    }
}

/// 设置中断亲和性
///
/// # Errors
/// 中断控制器或者中断号存在问题将会返回错误
pub fn irq_set_affinity(hwirq: u32, cpumask: Cpumask) -> Result<()> {
    unsafe { __irqchip_irq_set_affinity(hwirq, cpumask) }
}

/// 中断控制器设置中断类型
///
/// # Errors
/// 中断控制器或者中断号存在问题将会返回错误
pub fn irq_set_type(hwirq: u32, irq_type: IrqType) -> Result<()> {
    unsafe { __irqchip_irq_set_type(hwirq, irq_type) }
}

/// 中断控制器获取控制器状态
///
/// # Errors
/// 中断控制器或者中断号存在问题将会返回错误
pub fn irq_get_irqchip_state(hwirq: u32, which: IrqchipIrqState) -> Result<bool> {
    unsafe { __irqchip_irq_get_irqchip_state(hwirq, which) }
}

/// 中断控制器设置控制器状态
///
/// # Errors
/// 中断控制器或者中断号存在问题将会返回错误
pub fn irq_set_irqchip_state(hwirq: u32, which: IrqchipIrqState, state: bool) -> Result<()> {
    unsafe { __irqchip_irq_set_irqchip_state(hwirq, which, state) }
}
